#import "SDAutoLayout.h"

#import "LEEAlert.h"

#import "UpgradeView.h"

@interface UpgradeView ()

@property (nonatomic , strong ) UIImageView *imageView; //图片
@property (nonatomic , strong ) UILabel *titleLabel; //标题
@property (nonatomic , strong ) UILabel *contentLabel; //内容

@property (nonatomic , strong ) UIButton *settingButton; //设置按钮

@property (nonatomic , strong ) UIButton *colseButton; //关闭按钮

@end

@implementation UpgradeView


- (instancetype)initWithTitle:(NSString*)title
                         note:(NSString*)note
                        force:(BOOL)force{
    if(self = [super init]){
        CGFloat SCREEN_WIDTH =   [UIScreen mainScreen].bounds.size.width;
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH-40, 0);
        //初始化子视图
        [self initSubviewWithTitle:title note:note force:force];
        //设置自动布局
        [self configAutoLayoutWithForce:force];
    }
    
    return  self;
}


-(NSData *)getpng:(NSString*) name{
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"RNEasyui" ofType:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
    NSString *imagePath = [bundle pathForResource:name ofType:@"png"];
    NSData *data = [NSData dataWithContentsOfFile:imagePath];
    return  data;
}


#pragma mark - 初始化子视图

- (void)initSubviewWithTitle:(NSString*)title
                        note:(NSString*)note
                       force:(BOOL)force{
    
    // 图片
    
    _imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:[self getpng:@"upgrade_bg"]]];
    _imageView.contentMode = UIViewContentModeScaleAspectFit | UIViewContentModeTop | UIViewContentModeLeft | UIViewContentModeRight ;
    [self addSubview:_imageView];
    // 标题

    
    _titleLabel = [[UILabel alloc] init];
    
    _titleLabel.text = title;
    
    _titleLabel.textColor = [UIColor blackColor];
    
    _titleLabel.font = [UIFont systemFontOfSize:16.0f];
    
    _titleLabel.textAlignment = NSTextAlignmentCenter;
  _titleLabel.numberOfLines = 2;
    
    [self addSubview:_titleLabel];
    
    
    // 内容
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.text = note;
    _contentLabel.textColor = [UIColor colorWithRed:87/255.0f green:87/255.0f blue:87/255.0f alpha:1.0f];
    _contentLabel.font = [UIFont systemFontOfSize:14.0f];
    _contentLabel.numberOfLines = 0;
    [self addSubview:_contentLabel];
    
    // 设置按钮
    
    _settingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    _settingButton.sd_cornerRadius = @5.0f;
    
    [_settingButton.titleLabel setFont:[UIFont systemFontOfSize:16.0f]];
    
    [_settingButton setTitle:@"立即升级" forState:UIControlStateNormal];
    
    [_settingButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [_settingButton setBackgroundColor:[UIColor colorWithRed:233/255.0f green:101/255.0f blue:99/255.0f alpha:1.0f]];
    
  [_settingButton addTarget:self action:@selector(settingButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_settingButton];
    

    // 关闭按钮
    _colseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _colseButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    UIImage * colseButtonImage = [UIImage imageWithData:[self getpng:@"infor_colse_image"]];
    
    [_colseButton setImage: colseButtonImage forState:UIControlStateNormal];
    
    [_colseButton addTarget:self action:@selector(closeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_colseButton];
}

#pragma mark - 设置自动布局

- (void)configAutoLayoutWithForce:(BOOL)force{
    CGFloat SCREEN_WIDTH =   [UIScreen mainScreen].bounds.size.width;
    CGFloat W =SCREEN_WIDTH-60;
    // 图片
    
    self.imageView.sd_layout
    .topSpaceToView(self , 0.0f)
    .leftSpaceToView(self , 0.0f)
    .rightSpaceToView(self , 0.0f)
    .heightIs((SCREEN_WIDTH-60)/2.2);
    // 标题
    
    // 标题
    
    self.titleLabel.sd_layout
    .topSpaceToView(self.imageView , 0.0f)
    .centerXEqualToView(self)
    .widthIs(W)
    .minHeightIs(0.0f)
    .maxHeightIs(0.0f);
    
    // 内容

    self.contentLabel.sd_layout
    .topSpaceToView(self.titleLabel , 0.0f)
    .centerXEqualToView(self)
    .widthIs(W)
    .minHeightIs(0.0f)
    .maxHeightIs(120.0f);
    
    // 设置按钮
    
    self.settingButton.sd_layout
    .topSpaceToView(self.contentLabel , 10.0f)
    .leftSpaceToView(self , 30.0f)
    .rightSpaceToView(self , 30.0f)
    .heightIs(40.0f);
    
    // 关闭按钮
    if(!force){
        self.colseButton.sd_layout
        .topSpaceToView(self , .0f)
        .rightSpaceToView(self , .0f)
        .widthIs(44.0f)
        .heightIs(44.0f);
    }

    
    [self setupAutoHeightWithBottomView:self.settingButton bottomMargin:20.0f];
}

#pragma mark - 设置按钮点击事件

- (void)settingButtonAction:(UIButton *)sender{
    
  if(self.settingBlock) self.settingBlock();
    
}

#pragma mark - 关闭按钮点击事件

- (void)closeButtonAction:(UIButton *)sender{
    
    if (self.closeBlock) self.closeBlock();
}

@end
